package com.stillalive.springaop.proxy;

import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Buyer {

    public static void main(String[] args) {
        // 静态代理
        // 目标房子
        HouseSubject target = new RealHouseSubject();
//        // 代理对象
//        HouseSubject houseSubject = new HouseProxy(target);
//        houseSubject.saleHouse();
//        houseSubject.rentHouse();

        // JDK动态代理
//        HouseSubject houseProxy = (HouseSubject) Proxy.newProxyInstance(target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class}, new JDKInvocation(target));
//        houseProxy.rentHouse();

        // CGlib动态代理
        // 这里Enhancer要和CGlib中导入的版本匹配，导入相同的包
        HouseSubject houseProxy = (HouseSubject) Enhancer.create(target.getClass(), new CGlibMethodInterceptor(target));
        houseProxy.rentHouse();
    }
}
